package com.demo2do.fms.admin.service.supplier.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.admin.dao.supplier.ReformDao;
import com.demo2do.fms.admin.dao.supplier.SupplierDao;
import com.demo2do.fms.admin.entity.search.supplier.ReformSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.DataServiceImpl;
import com.demo2do.fms.admin.service.supplier.ReformService;
import com.demo2do.fms.entity.reform.Reform;
import com.demo2do.fms.entity.supplier.common.Supplier;

/**
 *
 * @author Ark
 *
 */
@Service("reformService")
public class ReformServiceImpl extends DataServiceImpl<Reform> implements ReformService {

    @Dao
    private ReformDao reformDao;
    
    @Dao
    private SupplierDao supplierDao;
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.RectifyService#count(com.demo2do.fms.admin.entity.security.Login)
     */
    public int count(Login login) {
        
        if (login.isSupplier()) {
            
            Supplier supplier = supplierDao.loadSupplier(login.getSupplierId());
            return reformDao.countByOriginate(supplier);
            
        } else {
            return reformDao.count();
        }
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.ReformService#list(com.demo2do.fms.admin.entity.security.Login, com.demo2do.core.entity.page.Page)
     */
    public List<Reform> list(Login login, Page page) {
        
        page.calculate();
        
        if (login.isSupplier()) {
            
            Supplier supplier = supplierDao.loadSupplier(login.getSupplierId());
            return reformDao.listByOriginate(supplier, page.getBeginIndex(), page.getEveryPage());
            
        } else {
            return reformDao.list(page.getBeginIndex(), page.getEveryPage());
        }
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.ReformService#count(com.demo2do.fms.admin.entity.search.supplier.ReformSearch)
     */
    public int count(ReformSearch search) {
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        return genericDaoSupport.count(sentence, parameters);
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.ReformService#search(com.demo2do.fms.admin.entity.search.supplier.ReformSearch, com.demo2do.core.entity.page.Page)
     */
    @SuppressWarnings("unchecked")
    public List<Reform> search(ReformSearch search, Page page) {
        
        // 1. build sentence
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        
        // 2. do query
        List<Reform> reforms = genericDaoSupport.searchForList(sentence, parameters, page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return reforms;
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.ReformService#create(com.demo2do.fms.entity.reform.Reform, com.demo2do.fms.admin.entity.security.Login)
     */
    public void create(Reform reform, Login login) {
        
        if (login.isSupplierUser()) {
            throw BusinessException.newInstance("500", "二级供方无法创建约谈整改");
        }
        
        if (login.isSupplier()) {
            Supplier originate = genericDaoSupport.load(Supplier.class, login.getSupplierId());
            genericDaoSupport.save(reform.initialize(originate, login.getId()));
        } else {
            genericDaoSupport.save(reform.initialize(login.getId()));
        }
        
    }
    
    /**
     * @param login
     * @return
     */
    public int countByRecipient(Login login) {
        if (login.isSupplier()) {
            Supplier supplier = supplierDao.loadSupplier(login.getSupplierId());
            return reformDao.countByRecipient(supplier);
        } else {
            return 0;
        }
    }
    
    /**
     * @param login
     * @param page
     * @return
     */
    public List<Reform> listByRecipient(Login login, Page page) {
        page.calculate();
        Supplier supplier = supplierDao.loadSupplier(login.getSupplierId());
        return reformDao.listByRecipient(supplier, page.getBeginIndex(), page.getEveryPage());
    }
    
}
